<?php

require_once 'Core/Controller/Action.php';

class Admin_NavigationController extends Core_Controller_Action
{
    /**
     * Initialize the action
     *
     * @return void
     */
    public function init()
    {
        parent::init();

        //the selected admin menu item
        $this->view->adminSection = 'navigation';
        $this->view->breadcrumbs = array(
           $this->view->getTranslation('Navigation') => $this->baseUrl . '/admin/navigation'
        );
    }

    /**
     * The default action
     *
     * Display the main menu admin page
     *
     * @return void
     */
    public function indexAction()
    {
        //open the main menu by default
        $this->_forward('open');
    }

    /**
     * Open action
     *
     * Open the specified menu
     *
     * @param int $id
     * @return void
     */
    public function openAction()
    {
        $mdlMenu = new Model_Menu();
        $menuId = $this->_request->getParam('id', 0);

        $label = 'Root';
        if ($menuId > 0) {
            $label = $mdlMenu->getLabel($menuId);
        }

        //fetch the menu
        $this->view->menuId = $menuId;
        $this->view->menu   = $mdlMenu->openMenu($menuId, true);

        $this->view->toolbarLinks = array();
        $this->view->toolbarLinks['Add to my bookmarks'] = $this->baseUrl . '/admin/index/bookmark'
            . '/url/admin_navigation_open_id_' . $menuId
            . '/label/' . $this->view->getTranslation('Navigation') . ':' . $label;
        $this->view->breadcrumbs[$this->view->getTranslation('Open Menu') . ': ' . $this->view->getTranslation($label)] = $this->baseUrl . '/admin/navigation/open/id/' . $menuId;
    }

    /**
     * Edit action
     *
     * Edit a menu
     *
     * @return void
     */
    public function editAction()
    {
        if ($this->_request->isPost()) {
            $mdlMenu = new Model_Menu();
            $ids = Core_Filter_Post::raw('id');
            $visibility = Core_Filter_Post::raw('show_on_menu');
            $mdlMenu->updateMenuItems($ids, $visibility);
            $menuId = Core_Filter_Post::get('menuId');
            $url = 'admin/navigation/open/id/' . $menuId;
            $this->_redirect($url);
        }
    }

    /**
     * Redirector action
     *
     * @return void
     */
    public function redirectorAction()
    {
        $r = new Model_Redirector();
        if ($this->_request->isPost()) {
            $request      = Core_Filter_Post::raw('request');
            $response     = Core_Filter_Post::raw('response');
            $responseCode = Core_Filter_Post::raw('response_code');
            $r->setFromArray($request, $response, $responseCode);
        }
        $this->view->redirectors = $r->fetchAll();
    }
}